//
//  EmergencyViewController.m
//  Qihuo
//
//  Created by ~Fighting~ on 13-10-20.
//  Copyright (c) 2013年 Vencoo. All rights reserved.
//
#import "DetailEmergencyViewController.h"
#import "JSON.h"
#import "UITableViewCell+new.h"
#import "EmergencyViewController.h"
#import "NSDictionary+new.h"
#import "NSDate+Calendar.h"
#import "PickerBackgroundView.h"
#import "BlackbackgroundView.h"
#import "EmergencyCell.h"
@interface EmergencyViewController ()<ASIHTTPRequestDelegate,UITableViewDataSource,UITableViewDelegate>
{
    ASIFormDataRequest *addEmergency;
    ASIFormDataRequest *queryEmergency;
    ASIFormDataRequest *finishEmergency;
    ASIHTTPRequest *searchRequest;
    
    UITableView *mainTable;
    UIScrollView *bgScrollView;
    UILabel *selTimeLb;
    UILabel *dateLabel;
    UITextField *placeTf;
    UITextView *systemTf;
    UITextView *reportTf;
    UITextView *dealTf;
    UITextView *resultTf;
    UITextView *analyseTf;
    UIAlertView *alertV;
    UIButton *submitBtn;
    UIButton *submitBtn1;
    UIButton *finishBtn;
    UIButton *pickTimeBtn;
    PickerBackgroundView *pickGroundV;
    PickerBackgroundView *pickerV1;
    BlackbackgroundView *blackGroundV;
    
    NSArray *queryListArr;
    
    
    NSMutableArray *addresses;
    NSMutableArray *analysises;
    NSMutableArray *crttimes;
    NSMutableArray *handles;
    NSMutableArray *ids;
    NSMutableArray *finishes;
    NSMutableArray *reportings;
    NSMutableArray *results;
    NSMutableArray *systemcases;
    NSMutableArray *updatetimes;
    NSMutableArray *userids;
    
}
@end

@implementation EmergencyViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"突发事件查询";
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardWillHideNotification object:nil];
    
    addresses = [NSMutableArray array];
    crttimes = [NSMutableArray array];
    finishes = [NSMutableArray array];
    ids = [NSMutableArray array];
    systemcases = [NSMutableArray array];
    reportings = [NSMutableArray array];
    handles = [NSMutableArray array];
    results = [NSMutableArray array];
    analysises = [NSMutableArray array];
 #pragma mark - 请求数据
    
    NSURL *queryUrl = [NSURL URLWithString:queryEmergencies_IP];
    queryEmergency = [[ASIFormDataRequest alloc] initWithURL:queryUrl];
    NSURL *finishUrl = [NSURL URLWithString:finishEmergencies];
    finishEmergency = [[ASIFormDataRequest alloc] initWithURL:finishUrl];
    
    [self analyseData];
    [self addSearch];
    [self addRightButton];
#pragma mark - 界面
    alertV = [[UIAlertView alloc]initWithTitle:@"警告" message:@"地点不能为空！" delegate:self cancelButtonTitle:@"继续填写" otherButtonTitles:nil, nil];
    
    UIImage *lineImage = [UIImage imageNamed:@"黑边框"];
    UIImage *whiteimage = [UIImage imageNamed:@"底框-白.png"];
    UIImageView *MyView = [[UIImageView alloc]initWithFrame:CGRectMake((scrennWidth - whiteimage.size.width)/2, 150, whiteimage.size.width, whiteimage.size.height)];
    MyView.image = whiteimage;
    [MyView setUserInteractionEnabled:YES];
    [self.view addSubview:MyView];
    
    UIImageView *lineView = [[UIImageView alloc]initWithImage:lineImage];
    lineView.frame = CGRectMake((whiteimage.size.width - lineImage.size.width)/2, (whiteimage.size.height - lineImage.size.height)/2, lineImage.size.width, lineImage.size.height);
    lineView.userInteractionEnabled = YES;
    [MyView addSubview:lineView];
    
    mainTable = [[UITableView alloc]initWithFrame:CGRectMake(2, 5, lineImage.size.width-6, lineImage.size.height-8) style:UITableViewStylePlain];
    mainTable.dataSource = self;
    mainTable.delegate = self;
    [lineView addSubview:mainTable];
    
    dateLabel = [UILabel new];
    NSDateFormatter *fom = [NSDateFormatter new];
    [fom setDateFormat:@"yyyy年MM月dd日"];
    dateLabel.text = [fom stringFromDate:[NSDate date]];
    dateLabel.font = [UIFont systemFontOfSize:35];
    dateLabel.backgroundColor = [UIColor clearColor];
    dateLabel.textColor = [UIColor whiteColor];
    CGSize size = [dateLabel.text sizeWithFont:dateLabel.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    dateLabel.frame = CGRectMake(33, (MyView.frame.origin.y - 44 - size.height)/2 + 44, size.width, size.height);
    [self.view addSubview:dateLabel];
    
    UIButton *calenderBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *calenderImage = [UIImage imageNamed:@"日历按钮"];
    UIImage *calenImage = [UIImage imageNamed:@"报修单日历按钮"];
    [calenderBtn setImage:calenderImage forState:UIControlStateNormal];
    [calenderBtn setFrame:CGRectMake(dateLabel.frame.origin.x + dateLabel.frame.size.width + interval111 * 2, (MyView.frame.origin.y - 44 -calenderImage.size.height)/2 + 44, calenderImage.size.width, calenderImage.size.height)];
    [calenderBtn addTarget:self action:@selector(pickDate1) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:calenderBtn];
    
#pragma mark -  增加突发事件
    blackGroundV = [[BlackbackgroundView alloc]initWithFrame:CGRectMake(0, 0, scrennWidth, screenHeight)];
    blackGroundV.userInteractionEnabled = YES;
    blackGroundV.delegate = self;
    [self.view addSubview:blackGroundV];
    
    bgScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(10, 10, [Appinfomation shareIstance].whiteView.frame.size.width - 20, [Appinfomation shareIstance].whiteView.frame.size.height - 20)];
    bgScrollView.contentSize = CGSizeMake(self.bgDelegate.whiteView.frame.size.width - 20, 750);
    bgScrollView.scrollEnabled = YES;
    [[Appinfomation shareIstance].whiteView addSubview:bgScrollView];
    UILabel *titleLb = [UILabel new];
    titleLb.text = @"突发事件记录";
    titleLb.font = [UIFont systemFontOfSize:35];
    titleLb.backgroundColor = [UIColor clearColor];
    CGSize sz = [titleLb.text sizeWithFont:titleLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    titleLb.frame = CGRectMake((blackGroundV.frame.size.width - sz.width)/2, 80, sz.width, sz.height);
    [blackGroundV addSubview:titleLb];
#pragma mark - 第一行
    //    时间
    UILabel *timeLb = [UILabel new];
    timeLb.text = @"时间：";
    timeLb.font = [UIFont systemFontOfSize:chara];
    timeLb.backgroundColor = [UIColor clearColor];
    sz = [timeLb.text sizeWithFont:timeLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    timeLb.frame = CGRectMake(interval111*1.4 ,interval111, sz.width, sz.height);
    [bgScrollView addSubview:timeLb];
    
    UIImageView *timeImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"底框 2"]];
    timeImageView.frame = CGRectMake(timeLb.frame.origin.x + timeLb.frame.size.width, interval111,(bgScrollView.frame.size.width - timeLb.frame.size.width - interval111*10)/2, sz.height);
    [bgScrollView addSubview:timeImageView];
    
    NSDateFormatter *dateFormatter = [NSDateFormatter new];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    selTimeLb = [UILabel new];
    selTimeLb.text = [dateFormatter stringFromDate:[NSDate date]];
    selTimeLb.font = [UIFont systemFontOfSize:chara-2];
    selTimeLb.backgroundColor = [UIColor clearColor];
//    [Appinfomation shareIstance].timeDateStr = selTimeLb.text;
    sz = [selTimeLb.text sizeWithFont:selTimeLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    selTimeLb.frame = CGRectMake((timeImageView.frame.size.width - sz.width)/2 ,1, sz.width, sz.height);
    [timeImageView addSubview:selTimeLb];
    
    pickTimeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [pickTimeBtn setImage:calenImage forState:UIControlStateNormal];
    [pickTimeBtn setFrame:CGRectMake(timeImageView.frame.origin.x + timeImageView.frame.size.width + 5 , (timeLb.frame.origin.y - (calenImage.size.height - timeImageView.frame.size.height)), calenImage.size.width, calenImage.size.height)];
    [bgScrollView addSubview:pickTimeBtn];
    //    地点
    UILabel *placeLb = [UILabel new];
    placeLb.text = @"地点：";
    placeLb.font = [UIFont systemFontOfSize:chara];
    placeLb.backgroundColor = [UIColor clearColor];
    sz = [placeLb.text sizeWithFont:placeLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    placeLb.frame = CGRectMake(bgScrollView.frame.size.width/2+interval111,timeLb.frame.origin.y, sz.width, sz.height);
    [bgScrollView addSubview:placeLb];
    
    UIImageView *placeImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"底框 2"]];
    placeImageView.frame = CGRectMake(placeLb.frame.origin.x + placeLb.frame.size.width, interval111,(bgScrollView.frame.size.width - timeLb.frame.size.width - interval111)/2 - 30, sz.height);
    [bgScrollView addSubview:placeImageView];
    placeImageView.userInteractionEnabled = YES;
    
    placeTf = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, placeImageView.frame.size.width, placeImageView.frame.size.height)];
    placeTf.contentHorizontalAlignment = 0;
    placeTf.font = [UIFont systemFontOfSize:chara-2];
    [placeImageView addSubview:placeTf];
    
#pragma mark - 第二行 系统影响情况
    UILabel *systemLb = [UILabel new];
    systemLb.text = @"系统影响情况：";
    systemLb.font = [UIFont systemFontOfSize:chara];
    systemLb.backgroundColor = [UIColor clearColor];
    sz = [systemLb.text sizeWithFont:systemLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    systemLb.frame = CGRectMake(timeLb.frame.origin.x,timeLb.frame.size.height + interval111 + timeLb.frame.origin.y, sz.width, sz.height);
    [bgScrollView addSubview:systemLb];
    
    UIImage *contentImage = [UIImage imageNamed:@"新闻内容"];
    UIImageView *systemView = [[UIImageView alloc]initWithFrame:CGRectMake(systemLb.frame.origin.x + systemLb.frame.size.width, systemLb.frame.origin.y, 650,100)];
    systemView.image = contentImage;
    [bgScrollView addSubview:systemView];
    
    systemTf = [[UITextView alloc]initWithFrame:CGRectMake(systemLb.frame.origin.x + systemLb.frame.size.width, systemLb.frame.origin.y, 650,100)];
    systemTf.font = [UIFont systemFontOfSize:22];
    [bgScrollView addSubview:systemTf];
    
    UILabel *reportLb = [UILabel new];
    reportLb.text = @"报告情况：";
    reportLb.font = [UIFont systemFontOfSize:chara];
    reportLb.backgroundColor = [UIColor clearColor];
    sz = [reportLb.text sizeWithFont:reportLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    reportLb.frame = CGRectMake(timeLb.frame.origin.x + chara*2,systemTf.frame.size.height + interval111 + systemTf.frame.origin.y, sz.width, sz.height);
    [bgScrollView addSubview:reportLb];
    
    UIImageView *reportView = [[UIImageView alloc]initWithFrame:CGRectMake(systemTf.frame.origin.x ,reportLb.frame.origin.y, systemTf.frame.size.width, systemTf.frame.size.height)];
    reportView.image = contentImage;
    [bgScrollView addSubview:reportView];
    
    reportTf = [[UITextView alloc]initWithFrame:CGRectMake(systemTf.frame.origin.x ,reportLb.frame.origin.y, systemTf.frame.size.width, systemTf.frame.size.height)];
    reportTf.font = [UIFont systemFontOfSize:22];
    [bgScrollView addSubview:reportTf];
    
    UILabel *dealLb = [UILabel new];
    dealLb.text = @"接到报告后处理：";
    dealLb.font = [UIFont systemFontOfSize:chara];
    dealLb.backgroundColor = [UIColor clearColor];
    sz = [dealLb.text sizeWithFont:dealLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    dealLb.frame = CGRectMake(timeLb.frame.origin.x - chara,reportTf.frame.size.height + interval111 + reportTf.frame.origin.y, sz.width, sz.height);
    [bgScrollView addSubview:dealLb];
    
    UIImageView *dealView = [[UIImageView alloc]initWithFrame:CGRectMake(reportTf.frame.origin.x ,dealLb.frame.origin.y, reportTf.frame.size.width, reportTf.frame.size.height)];
    dealView.image = contentImage;
    [bgScrollView addSubview:dealView];
    
    dealTf = [[UITextView alloc]initWithFrame:CGRectMake(reportTf.frame.origin.x ,dealLb.frame.origin.y, reportTf.frame.size.width, reportTf.frame.size.height)];
    dealTf.font = [UIFont systemFontOfSize:22];
    [bgScrollView addSubview:dealTf];
    
    UILabel *resultLb = [UILabel new];
    resultLb.text = @"结果：";
    resultLb.font = [UIFont systemFontOfSize:chara];
    resultLb.backgroundColor = [UIColor clearColor];
    sz = [resultLb.text sizeWithFont:resultLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    resultLb.frame = CGRectMake(timeLb.frame.origin.x + chara * 4,dealView.frame.size.height + interval111 + dealView.frame.origin.y, sz.width, sz.height);
    [bgScrollView addSubview:resultLb];
    
    UIImageView *resultView = [[UIImageView alloc]initWithFrame:CGRectMake(dealView.frame.origin.x, resultLb.frame.origin.y, reportTf.frame.size.width, reportTf.frame.size.height)];
    resultView.image = contentImage;
    [bgScrollView addSubview:resultView];
    resultView.userInteractionEnabled = YES;
    
    resultTf = [[UITextView alloc]initWithFrame:CGRectMake(dealView.frame.origin.x,resultLb.frame.origin.y, reportTf.frame.size.width, reportTf.frame.size.height)];
    resultTf.font = [UIFont systemFontOfSize:22];
    [bgScrollView addSubview:resultTf];
    
    UILabel *analyseLb = [UILabel new];
    analyseLb.text = @"分析：";
    analyseLb.font = [UIFont systemFontOfSize:chara];
    analyseLb.backgroundColor = [UIColor clearColor];
    sz = [analyseLb.text sizeWithFont:analyseLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    analyseLb.frame = CGRectMake(timeLb.frame.origin.x + chara * 4,resultView.frame.size.height + interval111 + resultView.frame.origin.y, sz.width, sz.height);
    [bgScrollView addSubview:analyseLb];
    
    UIImageView *analyseView = [[UIImageView alloc]initWithFrame:CGRectMake(resultView.frame.origin.x, analyseLb.frame.origin.y, resultTf.frame.size.width, resultTf.frame.size.height)];
    analyseView.image = contentImage;
    [bgScrollView addSubview:analyseView];
    analyseView.userInteractionEnabled = YES;
    
    analyseTf = [[UITextView alloc]initWithFrame:CGRectMake(resultView.frame.origin.x, analyseLb.frame.origin.y, reportTf.frame.size.width, reportTf.frame.size.height)];
    analyseTf.font = [UIFont systemFontOfSize:22];
    [bgScrollView addSubview:analyseTf];
    
    submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *subImage = [UIImage imageNamed:@"提交"];
    submitBtn.frame = CGRectMake((bgScrollView.frame.size.width - subImage.size.width)/2, analyseView.frame.origin.y + analyseView.frame.size.height + interval111, subImage.size.width, subImage.size.height);
    [submitBtn setImage:subImage forState:UIControlStateNormal];
    [submitBtn addTarget:self action:@selector(submit) forControlEvents:UIControlEventTouchUpInside];
    [bgScrollView addSubview:submitBtn];
    submitBtn.hidden = YES;
    
    submitBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    submitBtn1.frame = CGRectMake((bgScrollView.frame.size.width - (subImage.size.width*2))/3, submitBtn.frame.origin.y, subImage.size.width, subImage.size.height);
    [submitBtn1 setImage:subImage forState:UIControlStateNormal];
    [submitBtn1 addTarget:self action:@selector(update:) forControlEvents:UIControlEventTouchUpInside];
    [bgScrollView addSubview:submitBtn1];
    [submitBtn1 setHidden:YES];
    
    if ([Appinfomation shareIstance].strongXunJianCount == 6 ||[Appinfomation shareIstance].strongXunJianCount == 4)
    {
        submitBtn.hidden = NO;
        self.navigationController.navigationBar.hidden = YES;
    }
    else
    {
        blackGroundV.hidden = YES;
    }
    
    finishBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    finishBtn.frame = CGRectMake(submitBtn1.frame.size.width + submitBtn1.frame.origin.x +(subImage.size.width*2)/3, submitBtn1.frame.origin.y, subImage.size.width, subImage.size.height);
    [finishBtn setImage:[UIImage imageNamed:@"完成按钮"] forState:UIControlStateNormal];
    [finishBtn addTarget:self action:@selector(finish) forControlEvents:UIControlEventTouchUpInside];
    [bgScrollView addSubview:finishBtn];
    [finishBtn setHidden:YES];
    
    systemTf.delegate = self;
    reportTf.delegate = self;
    dealTf.delegate = self;
    resultTf.delegate = self;
    analyseTf.delegate = self;
#pragma mark - pickerView的添加
    pickGroundV = [[PickerBackgroundView alloc]initWithFrame:CGRectMake(0, 0, scrennWidth, screenHeight)];
    pickGroundV.userInteractionEnabled = YES;
    pickGroundV.butonDelegate = self;
    [pickGroundV canDate];
    pickGroundV.tag = 0;
    [self.view addSubview:pickGroundV];
    pickGroundV.hidden = YES;
    
    pickerV1 = [[PickerBackgroundView alloc]initWithFrame:CGRectMake(0, 0, scrennWidth, screenHeight)];
    pickerV1.userInteractionEnabled = YES;
    pickerV1.butonDelegate = self;
    pickerV1.tag = 1;
    [pickerV1 canDateAndTime];
    [self.view addSubview:pickerV1];
    pickerV1.hidden = YES;
    
}
#pragma mark - ASIHttpDelegate


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return addresses.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    EmergencyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (!cell) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"EmergencyCell" owner:self options:nil]lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dateLb.text = [crttimes objectAtIndex:indexPath.row];
    cell.InfoLb.text = [addresses objectAtIndex:indexPath.row];
    cell.cellBtn.tag = indexPath.row + 1;
    
    if ([[finishes objectAtIndex:indexPath.row]integerValue] == 1) {
        cell.cellBtn.hidden = YES;
    }
    cell.dele = self;
    cell.cellBtn.tag = indexPath.row;
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailEmergencyViewController *detailV = [[DetailEmergencyViewController alloc]init];
    [self.navigationController pushViewController:detailV animated:YES];
    detailV.emdDelegate = self;
    _queryDic = [queryListArr objectAtIndex:indexPath.row];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 56;
}
#pragma mark - UIScrollViewDelegate

#pragma mark - UITextViewDelegate
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if (textView.frame.origin.y + textView.frame.size.height + bgScrollView.frame.origin.y- bgScrollView.contentOffset.y + 5>= bgScrollView.frame.size.height - 406)
    {
        [bgScrollView setContentOffset:CGPointMake(bgScrollView.contentOffset.x, bgScrollView.contentOffset.y - (bgScrollView.frame.size.height - 406 + 10   )+(textView.frame.origin.y + textView.frame.size.height - bgScrollView.contentOffset.y)) animated:YES];
    }
    
}
#pragma mark - keyboardNotification
CGSize keyboardSize;

- (void) keyboardWasShown:(NSNotification *) notif{
    NSDictionary *info = [notif userInfo];
    NSValue *value = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
    keyboardSize = [value CGRectValue].size;
    NSLog(@"keyBoard:%f", keyboardSize.width);  //216
    bgScrollView.contentSize = CGSizeMake(self.bgDelegate.whiteView.frame.size.width - 20, 750+keyboardSize.width);
    
}
- (void) keyboardWasHidden:(NSNotification *) notif{
    NSDictionary *info = [notif userInfo];
    NSValue *value = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
    NSLog(@"keyboardWasHidden keyBoard:%f", keyboardSize.width);
    bgScrollView.contentSize = CGSizeMake(self.bgDelegate.whiteView.frame.size.width - 20, 750);
}


#pragma mark - buttonDelegate
- (void)sureOrCancelDelegate:(UIButton *)btn tag:(NSInteger)tag{
    if (tag == 0) {
        switch (btn.tag) {
            case 0:
            {
                if (pickGroundV.timeStr.length) {
                    dateLabel.text = pickGroundV.timeStr ;
                }else{
                    NSDateFormatter *fom = [NSDateFormatter new];
                    [fom setDateFormat:@"yyyy年MM月dd日"];
                    dateLabel.text = [fom stringFromDate:[NSDate date]];
                }
            }
                break;
                
            default:
                break;
        }
    }else{
        switch (btn.tag) {
            case 0:
            {
                if (pickerV1.timeDateStr.length) {
                    selTimeLb.text = pickerV1.timeDateStr;
                }else{
                    NSDateFormatter *dateFormatter = [NSDateFormatter new];
                    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
                    selTimeLb.text = [dateFormatter stringFromDate:[NSDate date]];
                }

            }
                break;
                
            default:
                break;
        }
    }
    pickGroundV.hidden = YES;
    pickerV1.hidden = YES;
    if (blackGroundV.hidden) {
        self.navigationController.navigationBar.hidden = NO;
    }
    
}


#pragma mark - button action
- (void)pickDate1
{
    pickGroundV.hidden = NO;
    self.navigationController.navigationBar.hidden = YES;
}
- (void)pickDate2
{
    pickerV1.hidden = NO;
    self.navigationController.navigationBar.hidden = YES;
}

- (void)submit
{
    if (placeTf.text.length == 0 ||systemTf.text.length ==0 ||reportTf.text.length == 0 || dealTf.text.length == 0 || resultTf.text.length == 0|| analyseTf.text.length == 0 ) {
        [alertV show];
    }else{
        NSString * url = [NSString stringWithFormat:addEmergencies,[systemTf.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[placeTf.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[reportTf.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[dealTf.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[resultTf.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[[Appinfomation shareIstance].loginDic valueForKey:@"id"],[selTimeLb.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[analyseTf.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[[Appinfomation shareIstance].loginDic valueForKey:@"departmentid"]];
        NSURL *newurl =  [[NSURL alloc] initWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:newurl];
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        [request setDelegate:self];
        [request setDidFinishSelector:@selector(requestDone:)];
        [request setDidFailSelector:@selector(requestWentWrong:)];
        [request startAsynchronous];
        
        NSString *response = [request responseString];
        NSLog(@"response111:%@",response);
        
        blackGroundV.hidden = YES;
        self.navigationController.navigationBar.hidden = NO;
        [[[UIApplication sharedApplication]keyWindow]endEditing:YES];
    }
    
}

-(void)update:(id)sender
{//?id=1&systemcase=突发事件&address=突发事件&reporting=机加酒&handle=的地地道道的&result=付个非官方 &analysis=121
    UIButton *btn = (UIButton*)sender;
    NSString *updateStr = [NSString stringWithFormat:updateEmergencies,ids[btn.tag],systemTf.text,placeTf.text,reportTf.text,dealTf.text,resultTf.text,analyseTf.text];
    NSURL *updateUrl = [[NSURL alloc] initWithString:[updateStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    ASIFormDataRequest *updateEmergency = [[ASIFormDataRequest alloc]initWithURL:updateUrl];

    updateEmergency.delegate = self;
    [updateEmergency setDidFinishSelector:@selector(updateSuccess)];
    [updateEmergency setDidFailSelector:@selector(updateFail)];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [updateEmergency startAsynchronous];
    [self analyseData];
}
- (void)finish
{
    [finishEmergency setPostValue:@"1" forKey:@"id"];
    [finishEmergency startSynchronous];
    
}
- (void)add:(UIButton *)btn
{
    if (btn.tag == 0) {
        submitBtn1.hidden = YES;
        finishBtn.hidden = YES;
        submitBtn.hidden = NO;
        placeTf.enabled = YES;
        [pickTimeBtn addTarget:self action:@selector(pickDate2) forControlEvents:UIControlEventTouchUpInside];
    }else{
        NSLog(@"tag = %d",btn.tag);
        selTimeLb.text = [crttimes objectAtIndex:btn.tag-1];
        placeTf.text = [addresses objectAtIndex:btn.tag-1];
        systemTf.text = [systemcases objectAtIndex:btn.tag-1];
        reportTf.text = [reportings objectAtIndex:btn.tag-1];
        dealTf.text = [handles objectAtIndex:btn.tag-1];
        resultTf.text = [results objectAtIndex:btn.tag-1];
        analyseTf.text = [analysises objectAtIndex:btn.tag-1];
        
        finishBtn.hidden = NO;
        submitBtn1.hidden = NO;
        submitBtn.hidden = YES;
        placeTf.enabled = NO;
        [pickTimeBtn removeTarget:self action:@selector(pickDate2) forControlEvents:UIControlEventTouchUpInside];
        
    }
    blackGroundV.hidden = NO;
    self.navigationController.navigationBar.hidden = YES;
}
- (void)searchInfo:(UIButton*)btn
{
    [[[UIApplication sharedApplication]keyWindow]endEditing:YES];
    NSString *yearStr = [dateLabel.text substringWithRange:NSMakeRange(0, 4)];
    NSString *monthStr = [dateLabel.text substringWithRange:NSMakeRange(5, 2)];
    NSString *dateStr = [dateLabel.text substringWithRange:NSMakeRange(8, 2)];
    NSString *timeStr = [NSString stringWithFormat:@"%@-%@-%@",yearStr,monthStr,dateStr];
//    NSURL *queryUrl = [NSURL URLWithString:queryEmergencies_IP];
    NSString * url = [NSString stringWithFormat:searchEmergency,timeStr,@"1",self.seachBar.text];
    NSURL *emerUrl =  [[NSURL alloc] initWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    searchRequest = [ASIHTTPRequest requestWithURL:emerUrl];
    [searchRequest setDelegate:self];
    [searchRequest setDidFinishSelector:@selector(requestDone1:)];
    [searchRequest setDidFailSelector:@selector(requestWentWrong:)];
    [searchRequest startAsynchronous];

    NSLog(@"%@",timeStr);
}
- (void)analyseData
{
    NSURL *queryUrl = [NSURL URLWithString:queryEmergencies_IP];
    queryEmergency = [[ASIFormDataRequest alloc] initWithURL:queryUrl];
    [queryEmergency setPostValue:@"" forKey:@"date"];
    [queryEmergency setPostValue:@"1" forKey:@"pageNo"];
    [queryEmergency setPostValue:@"" forKey:@"address"];
    [queryEmergency startSynchronous];
    
    NSString *response = [queryEmergency responseString];
    //    NSLog(@"response:%@",[response JSONValue]);
    NSDictionary *queryDic = [response JSONValue];
    NSDictionary *queryDataDic = [queryDic objectForKey:@"data"];
    queryListArr = [queryDataDic objectForKey:@"list"];
    NSLog(@"查询%@",queryListArr);
    [systemcases removeAllObjects];
    [addresses removeAllObjects];
    [crttimes removeAllObjects];
    [reportings removeAllObjects];
    [handles removeAllObjects];
    [results removeAllObjects];
    [analysises removeAllObjects];
    [finishes removeAllObjects];
    [ids removeAllObjects];
    for (NSDictionary *dic in queryListArr) {
        [systemcases addObject:[dic objectForKey:@"systemcase"]];
        [addresses addObject:[dic objectForKey:@"address"]];
        [crttimes addObject:[dic objectForKey:@"crttime"]];
        [reportings addObject:[dic objectForKey:@"reporting"]];
        [handles addObject:[dic objectForKey:@"handle"]];
        [results addObject:[dic objectForKey:@"result"]];
        [analysises addObject:[dic objectForKey:@"analysis"]];
        [finishes addObject:[dic objectForKey:@"isfinish"]];
        [ids addObject:[dic objectForKey:@"id"]];
    }
    [mainTable reloadData];
}
#pragma mark - requestDone
- (void)updateSuccess
{
    [MBProgressHUD hideHUDForView:self.view  animated:YES];
    UIAlertView *alerV = [[UIAlertView alloc]initWithTitle:@"提示" message:@"记录修改成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alerV show];
    blackGroundV.hidden = YES;
    self.navigationController.navigationBar.hidden = NO;
}
- (void)updateFail
{
    [MBProgressHUD hideHUDForView:self.view  animated:YES];
    UIAlertView *alerV = [[UIAlertView alloc]initWithTitle:@"提示" message:@"修改失败！！！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alerV show];
}
- (void)requestDone:(id)sender
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    [self analyseData];
}
- (void)requestDone1:(id)sender
{
    NSString *response = [searchRequest responseString];
    NSDictionary *queryDic = [response JSONValue];
    NSLog(@"dic=%@",response);
    NSDictionary *queryDataDic = [queryDic objectForKey:@"data"];
    queryListArr = [queryDataDic objectForKey:@"list"];
    NSLog(@"sousuo%@",queryListArr);
    [systemcases removeAllObjects];
    [addresses removeAllObjects];
    [crttimes removeAllObjects];
    [reportings removeAllObjects];
    [handles removeAllObjects];
    [results removeAllObjects];
    [analysises removeAllObjects];
    [finishes removeAllObjects];
    [ids removeAllObjects];
    for (NSDictionary *dic in queryListArr) {
        [systemcases addObject:[dic objectForKey:@"systemcase"]];
        [addresses addObject:[dic objectForKey:@"address"]];
        [crttimes addObject:[dic objectForKey:@"crttime"]];
        [reportings addObject:[dic objectForKey:@"reporting"]];
        [handles addObject:[dic objectForKey:@"handle"]];
        [results addObject:[dic objectForKey:@"result"]];
        [analysises addObject:[dic objectForKey:@"analysis"]];
        [finishes addObject:[dic objectForKey:@"isfinish"]];
        [ids addObject:[dic objectForKey:@"id"]];
    }
    [mainTable reloadData];

}
-(void)requestWentWrong
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"警告！" message:@"新增突发事件失败！！" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
    [alert show];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
